package com.aizhixin.lab.task.service;

import com.aizhixin.lab.task.domain.TaskFileDomain;
import com.aizhixin.lab.task.entity.TaskFile;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.aizhixin.lab.common.core.DataValidity;
import com.aizhixin.lab.task.domain.TaskQuestionChoiceDomain;
import com.aizhixin.lab.task.entity.TaskQuestionChoice;
import com.aizhixin.lab.task.repository.TaskQuestionChoiceRepository;

@Slf4j
@Service
@Transactional
public class TaskQuestionChoiceService {
	@Autowired
	private TaskQuestionChoiceRepository taskQuestionChoiceRepository;
	@Autowired
	private TaskFileService taskFileService;
//	@Autowired
//	private PageJdbcUtil pageJdbcUtil;

	
	public void save(TaskQuestionChoice choice) {
		taskQuestionChoiceRepository.save(choice);
	}
	
    public List<TaskQuestionChoice> getChoiceList(String taskQuestionId){
    	return taskQuestionChoiceRepository.findAllByDeleteFlagAndTaskQuestionId(DataValidity.VALID.getState(), taskQuestionId);

    }

	 public void saveChoiceList(List<TaskQuestionChoice> choiceList) {
		 taskQuestionChoiceRepository.save(choiceList);
	 }
	 
	 public void delChoiceList(List<TaskQuestionChoice> choiceList) {
		 taskQuestionChoiceRepository.delete(choiceList);
	 }
	 
	 public List<TaskQuestionChoiceDomain> getChoiceDomainList(Integer deleteFlag,String taskQuestionId){
		 List<TaskQuestionChoiceDomain> list =new ArrayList<>();
		 List<TaskQuestionChoiceDomain> all = taskQuestionChoiceRepository.findAll(deleteFlag, taskQuestionId);
		 for (TaskQuestionChoiceDomain t : all){
			 List<TaskFile> taskChoiceFile = taskFileService.findTaskChoiceFile(t.getId());
			 List<TaskFileDomain> taskFileDomains=new ArrayList<>();
			 for (TaskFile file:taskChoiceFile){
				 TaskFileDomain taskFileDomain = new TaskFileDomain();
				 BeanUtils.copyProperties(file,taskFileDomain);
				 taskFileDomains.add(taskFileDomain);
			 }
			 t.setAccessorylist(taskFileDomains);
			 list.add(t);
		 }
		 return list;
	 }
	 
	 public List<TaskQuestionChoice> findByDeleteFlagAndTaskQuestionIdIn(Integer deleteFlag,List<String> taskQuestionIds){
		 return taskQuestionChoiceRepository.findByDeleteFlagAndTaskQuestionIdIn(deleteFlag, taskQuestionIds);
	 }
	 
}
